/**
 * Title: BaseServiceImp.java
 * @author WuJin
 * @date 2020年3月31日
 * @version 1.0
 */
package com.pm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.mapper.BaseMapper;
import com.pm.service.BaseService;
import com.pm.utils.MergeObject;

import java.util.List;

/**
 * Title: BaseServiceImp
 * @author WuJin
 * @date 2020年3月31日
 */
public class BaseServiceImpl<T> implements BaseService<T> {
    private BaseMapper<T> getBaseMapper;

    public void setBaseMapper(BaseMapper<T> mapper) {
        getBaseMapper = mapper;
    }

    /* (non-Javadoc)
     * <p>Title: add</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @see com.pm.service.BaseService#add(java.lang.Object)
     */
    @Override
    public Integer add(T t) {
        return getBaseMapper.add(t);
    }

    /* (non-Javadoc)
     * <p>Title: update</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @see com.pm.service.BaseService#update(java.lang.Object)
     */
    @Override
    public Integer update(T t) {
        T old = getBaseMapper.loadById(t);
        MergeObject.combineSydwCore(t, old);
        int i = getBaseMapper.update(old);
        MergeObject.combineSydwCore(old, t);
        return i;
    }

    /* (non-Javadoc)
     * <p>Title: remove</p>
     * <p>Description: </p>
     * @param id
     * @return
     * @see com.pm.service.BaseService#remove(java.lang.Integer)
     */
    @Override
    public Integer remove(T t) {
        return getBaseMapper.remove(t);
    }

    /* (non-Javadoc)
     * <p>Title: load</p>
     * <p>Description: </p>
     * @param id
     * @return
     * @see com.pm.service.BaseService#load(java.lang.Integer)
     */
    @Override
    public T load(T t) {
        return getBaseMapper.load(t);
    }

    /* (non-Javadoc)
     * <p>Title: getList</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @see com.pm.service.BaseService#getList(java.lang.Object)
     */
    @Override
    public List<T> getList(T t) {
        return getBaseMapper.getList(t);
    }

    /* (non-Javadoc)
     * <p>Title: getListByPage</p>
     * <p>Description: </p>
     * @param t
     * @param page
     * @return
     * @see com.pm.service.BaseService#getListByPage(java.lang.Object, com.pm.common.Pages)
     */
    @Override
    public PageInfo<T> getListByPage(T t, Pages page) {
        PageHelper.startPage(page);
        PageInfo<T> pageInfo = new PageInfo<>(getBaseMapper.getList(t));
        PageHelper.clearPage();
        return pageInfo;
    }

}
